{extend name="public/base"/}
{block name="style"}
{/block}
{block name="content"}
<div class="data-list" data-url="{:url('index')}">
    <form class="layui-form inline-form">
        <div class="pull-left">
            <div class="layui-inline">
                <input class="layui-input laydate-range" autocomplete="off" name="create_time" placeholder="录入时间">
            </div>
            <div class="layui-inline">
                <button class="layui-btn layui-btn-sm layui-btn-normal search"><i class="layui-icon layui-icon-search"></i></button>
            </div>
        </div>
    </form>
    <div class="layui-row form-lists-button">
        <div class="layui-inline layui-btn-group">
            <a class="layui-btn layui-btn-normal layui-btn-sm" href="/attachment/template/product-develop.xlsx?v=1"><i class="layui-icon layui-icon-download-circle"></i>下载模板</a>
            <a class="layui-btn layui-btn-normal layui-btn-sm" id="import-develop"><i class="layui-icon layui-icon-upload-drag"></i>导入</a>
            <a class="layui-btn layui-btn-normal layui-btn-sm" id="export-develop"><i class="layui-icon layui-icon-export"></i>导出</a>
            <a class="layui-btn layui-btn-normal layui-btn-sm" id="calculate-develop"><i class="layui-icon layui-icon-cellphone-fine"></i>试算成本</a>
        </div>
    </div>
    <div class="data">
        <p><i class="fa fa-spinner fa-spin"></i> 加载中...</p>
    </div>
</div>
{/block}
{block name="script"}
<script type="text/javascript">
layui.use(['layer', 'jquery', 'upload'], function()
{
    var layer = layui.layer
        , upload = layui.upload
        , $ = layui.jquery;

    // 导入订单
    upload.render({
        elem: '#import-develop'
        , url: '{:url("file/upload",["type"=>"document"])}'
        , accept: 'file' //普通文件
        , exts: 'xlsx|xls|csv' // 指定类型
        , field: "file"
        , before: function (obj) {
            layer.load(0, {shade: false});
        }
        , done: function (response, index, upload)
        {
            if (response.code != 1)
            {
                layer.closeAll('loading');
                var icon = response.code == 1 ? 6 : 5;
                layer.msg(response.msg, {icon: icon, time: 1500});
                return;
            }
            $.post("{:url('wms/develop/import')}", {"path": response.data.src}).done(function (response)
            {
                layer.closeAll('loading');
                var icon = 5, time = 5500;
                if (response.code == 1)
                {
                    icon = 6;
                    time = 1500;
                }
                var icon = response.code == 1 ? 6 : 5;
                layer.msg(response.msg.replace(/\n/g, "<br/>"), {icon, time}, function(){
                    if (response.code == 1) $(".data").closest('.data-list').getList();
                });
            });
        }
        , error: function (index, upload)
        {
            layer.msg('上传失败');
        }
    });

    $("#export-develop").on("click", function()
    {
        var ids = [];
        $(".develop-id:checked").each(function()
        {
            ids.push($(this).val());
        });
        $.get("{:url('export')}", {ids: ids.join(",")}).done(function(response)
        {
            if(response.code !=1)
            {
                layer.msg(response.msg, {icon: 5, time: 1500});
                return;
            }
            location.href = "{:url('file/download')}?file=" + response.data.src
        });
    });

    // 试算成本
    $("#calculate-develop").click(function()
    {
        var $inputs = $("input.develop-id:checked");
        if($inputs.length < 1)
        {
            layer.msg("至少选择一个产品", {icon: 5, time: 1500});
            return;
        }
        var ids = [];
        $inputs.each(function()
        {
            ids.push($(this).val());
        });

        layer.open({
            type: 2,
            title: '试算成本',
            area: ['80%', '80%'],
            content: '{:url("calculate")}?ids=' + ids.join(",")
        });
    });

    // 认领产品
    $("body").on("click", ".claim-btn", function()
    {
       var id = $(this).attr("item");
        layer.prompt({
            title: '请输入销售账号'
            , maxlength: 10000000
            , formType: 3
            , value: ''
        }, function(text, index)
        {
            layer.close(index);
            $.post("{:url('claim')}", {"id": id, "account": text}).done(function(response)
            {
                var icon = response.code != 1 ? 5 : 6;
                layer.msg(response.msg, {icon, time: 1500});
                if (response.code == 1) $(".data").closest('.data-list').getList();
            });
        });
    });
});

</script>
{/block}